var baseUrl = require('./utils/urllink.js')
App({


  onLaunch: function () {
    console.log("app的onlaunch")
    //登录流程
    var that = this

  },

  onShow: function (options) {
    console.log("app的onShow")

  },

  getUserInfo: function (cb) {
    var that = this

    if (that.globalData.user_id) {
      console.log("全局变量中有openid")
      typeof cb == "function" && cb(that.globalData.userInfo, that.globalData.openid)
    } else {
      
        wx.login({//login流程
          success: function (res) {//登录成功
            if (res.code) {
              var code = res.code;
              wx.getUserInfo({//getUserInfo流程
                success: function (res2) {//获取userinfo成功
                  var encryptedData = res2.encryptedData;
                  var iv = res2.iv;
                  var signature = res2.signature;
                  var rawData = res2.rawData;
                  that.globalData.userInfo = res2.userInfo
                  //请求自己的服务器
                  wx.showToast({
                    title: '登录',
                    icon: 'loading',
                    //duration: 10000
                  });
                  //请求服务器
                  wx.request({
                    url: baseUrl+'weapp/LoginApi/weixin_login.html',
                    data: {
                      code: code,
                      encryptedData: encryptedData,
                      iv: iv,
                      rawData: rawData,
                      signature: signature,
                    },
                    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                    header: {'content-type': 'application/json'}, // 设置请求的 header
                    success: function (res) {
                      console.log('登录服务器返回');
                      console.log(res.data);
                      //更新用户数据 调用应用实例的方法获取全局数据
                      that.globalData.openid = res.data.openid
                      that.globalData.unionid = res.data.unionid
                      that.globalData.user_id = res.data.user_id
                      that.globalData.agent_type = res.data.agent_type //代理身份 0= 普通消费者  1= B级推广者 2= A级厂家
                      that.globalData.a_leader = res.data.a_leader
                      that.globalData.is_reg = res.data.is_reg
                      that.globalData.subscribe = res.data.subscribe
                      that.globalData.shop_id = res.data.shop_id

                      //console.log(res.data)
                      typeof cb == "function" && cb(that.globalData.userInfo, res.data.openid)
                    },
                    fail: function () {
                      wx.hideToast();
                      console.log('登录服务器失败');
                      wx.showModal({
                        title: '提示',
                        content: '登录服务器失败',
                        showCancel: false,
                        success: function (res) {
                          if (res.confirm) {
                            console.log('用户点击确定')
                          } 
                        }
                      })
                    },
                    complete: function () {}
                  })
                },
                fail: function () {
                  console.log("----------------2-------------")
                  // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
                  // wx.getSetting({
                  //   success(res) {
                  //     console.log(res)
                  //     console.log(res.authSetting['scope.userInfo'])
                  //     if (res.authSetting['scope.userInfo'] == false) {

                  //       wx.openSetting({
                  //         success: (res) => {
                  //           res.authSetting = {
                  //             "scope.userInfo": true
                  //           }
                  //           getApp().getUserInfo(function (userInfo, openid1) {
     

                  //           })
                  //         }
                  //       })
                  //     }
                  //   }
                  // })
                }
              })

            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          },
          fail: function () {
            
          }
        })
    }
  },

  globalData: {
    userInfo: null,
    openid: null,
    city_id: 2,
    user_id: 0,
    leader_id:0,
    suid: 0,
    subscribe: 1,
    a_leader:0,
    agent_type:0,
    is_reg:0,
    store_id:0,
    shop_id: 0,
    subscribe:0,
    mobile:null,
    hostimgs:'https://fs8.com',
    baseUser:'https://fs8.com/',
    duan_url:'https://fs8.com/u/',//短链接
  }
})

function getLogin(that){
    wx.login({//login流程
      success: function (res) {//登录成功
        if (res.code) {
          var code = res.code;
          wx.getUserInfo({//getUserInfo流程
            success: function (res2) {//获取userinfo成功
              var encryptedData = res2.encryptedData;
              var iv = res2.iv;
              var signature = res2.signature;
              var rawData = res2.rawData;
              that.globalData.userInfo = res2.userInfo
              //请求自己的服务器
              wx.showToast({
                title: '',
                icon: 'loading',
                //duration: 10000
              });
              //请求服务器
              wx.request({
                url: baseUrl + 'weapp/LoginApi/weixin_login.html',
                data: {
                  code: code,
                  encryptedData: encryptedData,
                  iv: iv,
                  rawData: rawData,
                  signature: signature,
                },
                method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                header: { 'content-type': 'application/json' }, // 设置请求的 header
                success: function (res) {
                  console.log('登录服务器返回');
                  console.log(res.data);
                  //更新用户数据 调用应用实例的方法获取全局数据
                  that.globalData.openid = res.data.openid
                  that.globalData.unionid = res.data.unionid
                  that.globalData.user_id = res.data.user_id
                  that.globalData.agent_type = res.data.agent_type //代理身份 0= 普通消费者  1= B级推广者 2= A级厂家
                  that.globalData.a_leader = res.data.a_leader
                  that.globalData.is_reg = res.data.is_reg
                  that.globalData.subscribe = res.data.subscribe
                  that.globalData.shop_id = res.data.shop_id
                  wx.setStorage({
                    key: "userinfolist",
                    data: res.data
                  })
                  //console.log(res.data)
                  typeof cb == "function" && cb(that.globalData.userInfo, res.data.openid)
                },
                fail: function () {
                  wx.hideToast();
                  console.log('登录服务器失败');
                  wx.showModal({
                    title: '提示',
                    content: '登录服务器失败',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      }
                    }
                  })
                },
                complete: function () { }
              })
            }
          })

        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  
}


